#include "xbee.h"
#include "serial.h"
#include "system.h"


Xbee::Xbee(Serial *_pSerial)
    :m_bRunning(true),
     m_pSerial(_pSerial)
{
}

Xbee::~Xbee()
{
    delete m_pSerial;
}

/* start reading from xbee */
void Xbee::run()
{
    while ( (m_bRunning == true) &&
            (m_pSerial->operational() == true) )
    {
        std::string ln = m_pSerial->readln(100);
        if (ln.length() > 0)
        {
            std::unique_lock<std::mutex> lock();
            m_queueLines.push_back(ln);
        }

        SYSTEM::sleep(10);
    }
}

/* flag stop */
void Xbee::stop()
{
    m_bRunning = false;
}

/* takes one line of text from xbee */
std::string Xbee::popString()
{
    std::string ret;
    std::unique_lock<std::mutex> lock();
    if (m_queueLines.empty() == false)
    {
        ret = m_queueLines.front();
        m_queueLines.pop_front();
    }

    return ret;
}

